﻿using Autofac.Integration.Web.Forms;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Timesheet.Service;
using Timesheet.Common;
using Timesheet.Domain;
using System.Net.Mail;
using System.Net;
using System.Drawing;

namespace Timesheet.WebForm.Account
{
    public partial class Forgotten : Page
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Login));
        
        public IUserService UserService { get; set;}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ASPxLabel1.Visible = false;
            }
        }

        protected void ASPxBtnLogin_Click(object sender, EventArgs e)
        {
            String username = ASPxTxtUserName.Text;

            User user = UserService.getAllUsers().FirstOrDefault(u => u.Username == username);

            if (user != null)
            {

                var fromAddress = new MailAddress("test1@mnspos.com", "Titan Support");
                String senderName = user.FirstName + " " + user.LastName;
                var toAddress = new MailAddress(user.Username, senderName);

                String newpass = PasswordUtils.RandomString(6);
                const string fromPassword = "123456@Ab";
                const string subject = "[TiTan] Your Reset Password";

                String body = @"Hi, <br/> 
                            Your new Password is: " + newpass + ". Please change your new password ASAP. ";

                body = body + "<br/>  Thanks, <br/> Titan Team";

                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = true
                })
                {
                    smtp.Send(message);
                }

                
                ASPxLabel1.Visible = true;
                ASPxLabel1.Text = "New password is sent to your email";
                ASPxLabel1.ForeColor = Color.Blue;
                user.Password = newpass;
                UserService.Update(user);
            }
            else
            {
                ASPxLabel1.Visible = true;
                ASPxLabel1.Text = "This email address is not exist in our system.Pls try again";
                ASPxLabel1.ForeColor = Color.Red;
            }
        }
    }
    
}